Completed
Push — master ( 9899df...273a12 )
by
unknown
02:23
created

meta.js ➔ add   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
c 1
b 0
f 0
nc 8
dl 0
loc 24
rs 5.2164
nop 6

How to fix   Complexity   

Complexity

Complex classes like meta.js ➔ add often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import extend from 'extend'
2
import {
3
  cmsData
4
  ,config
5
  ,dateSlug
6
} from '../../'
7
8
export function add(tpl, json, type, obj = {}, date = null, realType = 'draft') {
9
  let meta = config.meta.name
10
11
  // json[meta] = extend({}, json[meta])
12
  var currentDate = (typeof date !== 'undefined' && date !== null && date !== '') ? date : new Date()
13
  var abeUrl = (type === 'publish') ? json[meta].link : cmsData.fileAttr.add(json[meta].link, 'd' + dateSlug(currentDate.toISOString())) + ''
14
15
  if(typeof json[meta].date === 'undefined' || json[meta].date === null) {
16
    json[meta].date = currentDate
17
  }
18
  json[meta].latest = {
19
    date: currentDate,
20
    abeUrl: abeUrl
21
  }
22
  json[meta].status = realType === 'reject' ? 'draft' : realType
23
  if(typeof json[meta][type] === 'undefined' || json[meta][type] === null) {
24
    json[meta][type] = JSON.parse(JSON.stringify(obj))
25
    json[meta][type].date = currentDate
26
    json[meta][type].abeUrl = abeUrl
27
  }
28
  json[meta][type].latest = JSON.parse(JSON.stringify(obj))
29
  json[meta][type].latest.date = currentDate
30
  json[meta][type].latest.abeUrl = abeUrl
31
}